#!/bin/sh
# This regression test is a part of SIPp.
# -timeout_error makes a busy TCP UAS exit with the timeout error. The
# SIGALRM handler used to report it and exit right there, in whatever
# the main thread was doing, often a malloc: a corrupted heap, SIGABRT
# or SIGSEGV. A race, so several busy UAS time out, a few rounds.
. "`dirname "$0"`/../functions"; init

err=`mktemp`
trap 'rm -f "$err" "$err".*' EXIT

for round in 0 1 2 3 4 5; do
    uas=
    for i in 0 1 2 3; do
        port=$((23100 + (round * 4 + i) * 2))
        "$SIPP" -sn uas -t t1 -i 127.0.0.1 -p $port -nostdin \
            -timeout 1 -timeout_error </dev/null >/dev/null 2>"$err.$i" &
        uas="$uas $!"
    done
    sleep 0.2
    for i in 0 1 2 3; do
        port=$((23100 + (round * 4 + i) * 2))
        "$SIPP" -sn uac -t t1 -i 127.0.0.1 -p $((port + 1)) -r 15000 \
            -l 20000 -d 5000 -nostdin -timeout 5 127.0.0.1:$port \
            </dev/null >/dev/null 2>&1 &
    done
    i=0
    for pid in $uas; do
        wait $pid
        status=$?
        grep -q "timed out after" "$err.$i" ||
            fail "UAS exited with $status: `head -1 "$err.$i"`"
        rm -f "$err.$i"
        # EXIT_FATAL_ERROR
        test $status -eq 255 || fail "UAS exited with $status"
        i=$((i + 1))
    done
    pkill -KILL -P$$
    wait
done
ok
